import Vue from 'vue'
import VueRouter from 'vue-router'
//引入仓库。因为路由守卫里面需要存数据
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {path:"/",
  component:()=>import("../pages/Layout"),
  meta:{selected:"/"},
  children:[
    {path:"index",component:()=>import("../views/index"),meta:{selected:"/"}},
    {path:"menu",component:()=>import("../views/menu"),meta:{selected:"/menu"}},
    {path:"role",component:()=>import("../views/role"),meta:{selected:"/role"}},
    {path:"user",component:()=>import("../views/user"),meta:{selected:"/user"}},
    {path:"sort",component:()=>import("../views/sort"),meta:{selected:"/sort"}},
    {path:"specs",component:()=>import("../views/specs"),meta:{selected:"/specs"}},
    {path:"goods",component:()=>import("../views/goods"),meta:{selected:"/goods"}},
    {path:"seck",component:()=>import("../views/seck"),meta:{selected:"/seck"}},
    {path:"banner",component:()=>import("../views/banner"),meta:{selected:"/banner"}},
    {path:"member",component:()=>import("../views/member"),meta:{selected:"/member"}},
    {path:"",redirect:"/index"}
  ]

},
  {path:"/login",component:()=>import("../pages/Login"),meta:{selected:"/login"}},
  {path:"*",redirect:"/",}
]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{

    if(to.fullPath === "/login"){
       next()
       return 
    }
    if(to.fullPath !== "/login"){
      let userinfo = store.state.userInfo;
      if(userinfo != null){
        //验证用户访问的路由规则 是否在已分配的路由规则中
         let nowpath = to.fullPath;
         let allowpath = userinfo.menus_url;
         if(allowpath.indexOf("/") < 0 && allowpath.indexOf("/index") < 0){
               allowpath.push("/");
               allowpath.push("/index");
         }
         
         if(allowpath.indexOf(nowpath) >= 0){
                next()
         }else{
             next(false)
         }
      }else{
         next("/login")
      }
    }
})
export default router
